package com.controller;

import com.entity.BusinessProjectInfo;
import com.service.BusinessProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【经营项目】
 */

@RestController
@RequestMapping("/api/business/project")
public class BusinessController {

    @Autowired
    private BusinessProjectInfoService businessProjectInfoService;

    /**
     * 创建经营项目信息
     * @param businessProjectInfo 经营项目信息
     * @return 创建后的经营项目信息
     */
    @PostMapping
    public BusinessProjectInfo createBusinessProject(@RequestBody BusinessProjectInfo businessProjectInfo) {
        return businessProjectInfoService.save(businessProjectInfo);
    }

    /**
     * 删除经营项目信息
     * @param id 经营项目ID
     */
    @DeleteMapping("/{id}")
    public void deleteBusinessProject(@PathVariable Integer id) {
        businessProjectInfoService.deleteById(id);
    }

    @PutMapping("/{id}")
    public BusinessProjectInfo updateBusinessProject(@PathVariable Long id, @RequestBody BusinessProjectInfo businessProjectInfo) {
        businessProjectInfo.setId(Math.toIntExact(id));
        return businessProjectInfoService.update(businessProjectInfo);
    }

    /**
     * 根据ID获取经营项目信息
     * @param id 经营项目ID
     * @return 经营项目信息
     */
    @GetMapping("/{id}")
    public BusinessProjectInfo getBusinessProjectById(@PathVariable Integer id) {
        return businessProjectInfoService.findById(id);
    }

    @GetMapping
    public List<BusinessProjectInfo> getAllBusinessProjects() {
        return businessProjectInfoService.findAll();
    }

    @GetMapping("/page")
    public Page<BusinessProjectInfo> getBusinessProjectsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return businessProjectInfoService.findAll(pageable);
    }
}